﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace QLBN
{
    public partial class frmmain : Form
    {
        DateTime dtime;
        String strhour;
        private String _username;
        frmdangnhap frmdn = new frmdangnhap();
        frmTTBenhNhan frmttbenhnhan;
        public Boolean ckfrmbenhnhanactive = false;     //check frmTTBenhNhan active
        //frmDSBenhNhan frmdsbn;
        public String Username
        {
            get { return _username; }
            set { _username = value; }
        }
        private int _role;

        public int Role
        {
            get { return _role; }
            set { _role = value; }
        }

        private Boolean _status;

        public Boolean Status
        {
            get { return _status; }
            set { _status = value; }
        }

        public frmmain()
        {
            InitializeComponent();
        }

        private void frmmain_Load(object sender, EventArgs e)
        {
            //Check
            //Menuqlhethong.Visible = true;
            //Menuqlbenhnhan.Visible = true;
            this.timbn_MenuItem.Enabled = true;
            
            
        }

        private void Menuthoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void Menudangnhap_Click(object sender, EventArgs e)
        {
            //frmdn.MdiParent = this;
            frmdn.ShowDialog();
            _username = frmdn.Username;
            _role = frmdn.Role;
            _status = frmdn.Status;
            if (_status == true)
            {
                this.Menudangxuat.Visible = true;
                this.Menudangnhap.Visible = false;
                this.Menuthongtincanhan.Visible = true;
                if (_role == 1)
                {
                    Menuqlhethong.Visible = true;
                    Menuqlbenhnhan.Visible = true;
                }
                else if (_role == 2)
                {
                    Menuqlbenhnhan.Visible = true;
                }
                lbhello.Text += _username;
            }
            
        }

        private void Menudangxuat_Click(object sender, EventArgs e)
        {
            this.Menudangxuat.Visible = false;
            this.Menudangnhap.Visible = true;
            this.Menuthongtincanhan.Visible = false;
            this.Menuqlhethong.Visible = false;
            this.Menuqlbenhnhan.Visible = false;
            frmdn.txtpass.Text = String.Empty;
            frmdn.txtusername.Text = String.Empty;

            lbhello.Text = "Chào mừng ";
        }

        private void Menuthongtincanhan_Click(object sender, EventArgs e)
        {
            frmTTCN frm = new frmTTCN();
            frm.Username = this._username;
            //frm.MdiParent = this;
            frm.ShowDialog();
            _username = frm.Username;

        }

        private void Menuqlbenhnhan_Click(object sender, EventArgs e)
        {

            //frmdsbn = new frmDSBenhNhan();
            //frmdsbn.MdiParent = this;
            //frmdsbn.Show();
            if (ckfrmbenhnhanactive == false)
            {
                frmttbenhnhan = new frmTTBenhNhan(this);
                frmttbenhnhan.MdiParent = this;
                frmttbenhnhan.Show();
                ckfrmbenhnhanactive = true;
                this.saveMenuItem.Enabled = true;
                this.timbn_MenuItem.Enabled = true;
                this.changeToolStripMenuItem.Enabled = true;
                this.ThemBNToolStripMenuItem.Enabled = true;
                //this.Soantoathuoc_MenuItem.Enabled = true;
            }
            //else
            //{
            //    //this.saveMenuItem.Enabled = false;
            //}

        }

        private void frmmain_Resize(object sender, EventArgs e)
        {
            if (this.Width >= 450)
            {
                this.lbhello.Location = new Point(this.Width - 150, this.lbhello.Location.Y - 4);
            }
        }

        private void menuuser_Click(object sender, EventArgs e)
        {
            frmQLUser frmuser = new frmQLUser();
            frmuser.CurrentUser = _username;
            frmuser.MdiParent = this;
            frmuser.Show();
        }

        private void Menutrogiup_Click(object sender, EventArgs e)
        {

        }

        private void menuthuoc_Click(object sender, EventArgs e)
        {

        }

        private void menuabout_Click(object sender, EventArgs e)
        {
            frmAbout frma = new frmAbout();
            frma.MdiParent = this;
            frma.Show();
        }

        private void saveMenuItem_Click(object sender, EventArgs e)
        {
            if (frmttbenhnhan.cktab == 0)
            {
                frmttbenhnhan.Cap_Nhat_Thong_Tin_Benh_Nhan();
            }
            else if (frmttbenhnhan.cktab == 1)
            {
                frmttbenhnhan.Cap_Nhat_Toa_Thuoc();
            }
            frmttbenhnhan.cksuadl = false;
        }

        private void timbn_MenuItem_Click(object sender, EventArgs e)
        {
            //frmttbenhnhan.Setfocus_Ten_Benh_Nhan();


            if (BenhNhan.cksuadl == true)
            {
                if (MessageBox.Show("Bạn tìm bệnh nhân mới mà không lưu bệnh nhân hiện tại?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Information, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
                {
                    frmDSBenhNhan frmdsbn = new frmDSBenhNhan();
                    frmdsbn.ShowDialog();
                    String MaBN = frmdsbn.MaBN;
                    if (ckfrmbenhnhanactive == false)
                    {
                        frmttbenhnhan = new frmTTBenhNhan(this);
                        frmttbenhnhan.MdiParent = this;
                        frmttbenhnhan.Show();
                        ckfrmbenhnhanactive = true;
                    }
                    frmttbenhnhan.Activate();
                    frmdsbn.Close();
                    if (!MaBN.Equals("-1"))
                    {
                        frmttbenhnhan.Hien_Thong_Tin(MaBN);
                        this.saveMenuItem.Enabled = true;
                        this.timbn_MenuItem.Enabled = true;
                        this.changeToolStripMenuItem.Enabled = true;
                        this.ThemBNToolStripMenuItem.Enabled = true;
                    }
                }
            }
            else
            {
                frmDSBenhNhan frmdsbn = new frmDSBenhNhan();
                frmdsbn.ShowDialog();
                String MaBN = frmdsbn.MaBN;
                frmdsbn.Close();
                BenhNhan.IsClose = true;
                if (ckfrmbenhnhanactive == false)
                {
                    frmttbenhnhan = new frmTTBenhNhan(this);
                    frmttbenhnhan.MdiParent = this;
                    if ((BenhNhan.IsClose == true) && (frmDSBenhNhan.IsClick == true))
                    {
                        frmttbenhnhan.Show();
                        ckfrmbenhnhanactive = true;
                    }  
                }
                frmttbenhnhan.Activate();
                
                if (!MaBN.Equals("-1"))
                {
                    frmttbenhnhan.Hien_Thong_Tin(MaBN);
                    this.saveMenuItem.Enabled = true;
                    this.timbn_MenuItem.Enabled = true;
                    this.changeToolStripMenuItem.Enabled = true;
                    this.ThemBNToolStripMenuItem.Enabled = true;
                    
                }
            }

        }

        private void Soantoathuoc_MenuItem_Click(object sender, EventArgs e)
        {
            frmttbenhnhan.Soan_Toa_Thuoc();
        }

        private void changeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (frmttbenhnhan.tabCtrbenhnhan.SelectedIndex == 0)
            {
                frmttbenhnhan.tabCtrbenhnhan.SelectedIndex = 1;
            }
            else
            {
                frmttbenhnhan.tabCtrbenhnhan.SelectedIndex = 0;
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            dtime = DateTime.Now;
            strhour = Them_So(dtime.Hour)+":";
            strhour += Them_So(dtime.Minute)+":";
            strhour += Them_So(dtime.Second);
            this.lbhello.Text = strhour;
        }
        private String Them_So(int so)
        {
            String str = so.ToString(); ;
            if (so < 10)
            {
                str = "0" + so;
            }
            return str;
        }

        private void Menu_chucnang_Click(object sender, EventArgs e)
        {

        }

        private void frmmain_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (MessageBox.Show("Bạn thật sự muốn thoát khởi chương trình !", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Information, MessageBoxDefaultButton.Button2) == DialogResult.No)
            {
                e.Cancel = true;
            }
        }

        private void ThemBNToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (frmttbenhnhan.cksuadl == true)
            {
                if (MessageBox.Show("Bạn thêm bệnh nhân mới mà không lưu bệnh nhân hiện tại?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Information, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
                {
                    //this.saveMenuItem_Click(sender, e);
                    frmttbenhnhan.Setfocus_Select_Ten_Benh_Nhan();
                }
            }
            else
            {
                frmttbenhnhan.Setfocus_Select_Ten_Benh_Nhan();
            }
            
        }

        private void lbhello_Click(object sender, EventArgs e)
        {

        }
    }
}
